//
//  CTTopicView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/15.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTTopicView.h"
#import "CTHeader.h"
#import "CTDebugManager.h"
#import "CTDiscoveryItems.h"
#import "CTTextManager.h"
#import "MLEmojiLabel.h"


@interface CTTopicView()<MLEmojiLabelDelegate>
{
    MLEmojiLabel * emojiLabel;
    CTFollowList *follwItem;
    NSArray * topicArray;
    
    CGRect currentFrame;
}

@end

@implementation CTTopicView
@synthesize delegate;

- (void)dealloc
{
    [emojiLabel removeFromSuperview];
    emojiLabel.delegate = nil;
    emojiLabel = nil;
    delegate = nil;
    follwItem = nil;
    topicArray = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTTopicView Is Removed!!!"];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
     
        currentFrame = frame;
        
        self.backgroundColor = [UIColor clearColor];
        
       // [self createTopicView];
    }
    return self;
}

- (void)createTopicView
{
 
//    emojiLabel = [[MLEmojiLabel alloc]init];
//    emojiLabel.numberOfLines = 0;
//    emojiLabel.font = [UIFont systemFontOfSize:15.0f];
//    emojiLabel.emojiDelegate = self;
//    emojiLabel.backgroundColor = [UIColor clearColor];
//    emojiLabel.lineBreakMode = NSLineBreakByCharWrapping;
//    emojiLabel.isNeedAtAndPoundSign = YES;
//    [self addSubview:emojiLabel];
    
}

-(void)creatTopicView:(CTFollowList *)itm
{
    if (emojiLabel != nil) {
        [emojiLabel removeFromSuperview];
        emojiLabel = nil;
        emojiLabel.delegate = nil;
    }
    
    emojiLabel = [[MLEmojiLabel alloc]init];
//    emojiLabel.numberOfLines = 0;
    emojiLabel.font = [UIFont systemFontOfSize:15.0f];
//    emojiLabel.emojiDelegate = self;
    emojiLabel.backgroundColor = [UIColor clearColor];
//    emojiLabel.lineBreakMode = NSLineBreakByCharWrapping;
    emojiLabel.isNeedAtAndPoundSign = YES;
    [self addSubview:emojiLabel];
    
    
    @try {
        
        follwItem = itm;
        NSString * describeStr = [NSString string];
        topicArray = itm.topic_list;
        NSMutableAttributedString * describeAttributeStr;
        if (topicArray.count == 0) {
            
            
            if ([[CTTextManager shareInstance] isBlankString:itm.text] == NO)
            {
                describeStr = [describeStr stringByAppendingString:itm.text];
                describeAttributeStr = [[NSMutableAttributedString alloc] initWithString:describeStr];
                [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(0,itm.text.length)];
                [emojiLabel showAttributeLabel:describeAttributeStr];
                describeStr = nil;
            }
            else{
                self.frame = CGRectMake(currentFrame.origin.x, currentFrame.origin.y,CT_HEADER_SCREEN_WIDTH, 0);
                return;
            }
            
        }
        else if (topicArray.count == 1)
        {
            
            CTTopic * topicItem_1 = (CTTopic *)[topicArray objectAtIndex:0];
            NSString * sharp_1 = [NSString string];
            if ([[CTTextManager shareInstance] isBlankString:topicItem_1.topic] == NO)
            {
                sharp_1 = [NSString stringWithFormat:@"#%@#",topicItem_1.topic];
                describeStr = [describeStr stringByAppendingString:sharp_1];
            }
            
            if ([[CTTextManager shareInstance] isBlankString:itm.text] == NO);
            {
                describeStr = [describeStr stringByAppendingString:itm.text];
            }
            
            describeAttributeStr = [[NSMutableAttributedString alloc] initWithString:describeStr];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255.0 green:212/255.0 blue:103/255.0 alpha:1.0] range:NSMakeRange(0,sharp_1.length)];
            
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(0,sharp_1.length)];
            
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(sharp_1.length,itm.text.length)];
            
            
            [emojiLabel showAttributeLabel:describeAttributeStr];
            
            describeStr = nil;
            sharp_1 = nil;
            
            
        }
        else if (topicArray.count == 2)
        {
            
            CTTopic * topicItem_1 = (CTTopic *)[topicArray objectAtIndex:0];
            NSString * sharp_1 = [NSString string];
            if ([[CTTextManager shareInstance] isBlankString:topicItem_1.topic] == NO)
            {
                sharp_1 = [NSString stringWithFormat:@"#%@#",topicItem_1.topic];
                describeStr = [describeStr stringByAppendingString:sharp_1];
            }
            
            CTTopic * topicItem_2 = (CTTopic *)[topicArray objectAtIndex:1];
            NSString * sharp_2 = [NSString string];
            if ([[CTTextManager shareInstance] isBlankString:topicItem_2.topic] == NO)
            {
                sharp_2 = [NSString stringWithFormat:@"#%@#",topicItem_2.topic];
                describeStr = [describeStr stringByAppendingString:sharp_2];
            }
            
            
            if ([[CTTextManager shareInstance] isBlankString:itm.text] == NO);
            {
                describeStr = [describeStr stringByAppendingString:itm.text];
            }
            
            describeAttributeStr = [[NSMutableAttributedString alloc] initWithString:describeStr];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255.0 green:212/255.0 blue:103/255.0 alpha:1.0] range:NSMakeRange(0,sharp_1.length)];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255.0 green:212/255.0 blue:103/255.0 alpha:1.0] range:NSMakeRange(sharp_1.length,sharp_2.length)];
            
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(0,sharp_1.length)];
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(sharp_1.length,sharp_2.length)];
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(sharp_1.length + sharp_2.length,itm.text.length)];
            
            
            [emojiLabel showAttributeLabel:describeAttributeStr];
            
            
            describeStr = nil;
            sharp_1 = nil;
            sharp_2 = nil;
            
            
            
        }else {
            
            CTTopic * topicItem_1 = (CTTopic *)[topicArray objectAtIndex:0];
            NSString * sharp_1 = [NSString string];
            if ([[CTTextManager shareInstance] isBlankString:topicItem_1.topic] == NO)
            {
                sharp_1 = [NSString stringWithFormat:@"#%@#",topicItem_1.topic];
                describeStr = [describeStr stringByAppendingString:sharp_1];
            }
            
            CTTopic * topicItem_2 = (CTTopic *)[topicArray objectAtIndex:1];
            NSString * sharp_2 = [NSString string];
            if ([[CTTextManager shareInstance] isBlankString:topicItem_2.topic] == NO)
            {
                sharp_2 = [NSString stringWithFormat:@"#%@#",topicItem_2.topic];
                describeStr = [describeStr stringByAppendingString:sharp_2];
            }
            
            CTTopic * topicItem_3 = (CTTopic *)[topicArray objectAtIndex:2];
            NSString * sharp_3 = [NSString string];
            if ([[CTTextManager shareInstance] isBlankString:topicItem_3.topic] == NO)
            {
                sharp_3 = [NSString stringWithFormat:@"#%@#",topicItem_3.topic];
                describeStr = [describeStr stringByAppendingString:sharp_3];
            }
            
            
            if ([[CTTextManager shareInstance] isBlankString:itm.text] == NO);
            {
                describeStr = [describeStr stringByAppendingString:itm.text];
            }
            
            describeAttributeStr = [[NSMutableAttributedString alloc] initWithString:describeStr];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255.0 green:212/255.0 blue:103/255.0 alpha:1.0] range:NSMakeRange(0,sharp_1.length)];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255.0 green:212/255.0 blue:103/255.0 alpha:1.0] range:NSMakeRange(sharp_1.length,sharp_2.length)];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255.0 green:212/255.0 blue:103/255.0 alpha:1.0] range:NSMakeRange(sharp_1.length + sharp_2.length,sharp_3.length)];
            
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(0,sharp_1.length)];
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(sharp_1.length,sharp_2.length)];
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(sharp_1.length + sharp_2.length,sharp_3.length)];
            
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(sharp_1.length + sharp_2.length + sharp_3.length,itm.text.length)];
            
            
            [emojiLabel showAttributeLabel:describeAttributeStr];
            
            describeStr = nil;
            sharp_1 = nil;
            sharp_2 = nil;
            sharp_3 = nil;
            
        }
//        else
//        {
//            describeAttributeStr = [[NSMutableAttributedString alloc] initWithString:@""];
//            [emojiLabel showAttributeLabel:describeAttributeStr];
//            
//            describeStr = nil;
//        }
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"话题拼接出现崩溃错误！"];
    }
    
    
    emojiLabel.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH-20, 15);
 //   [emojiLabel sizeToFit];
    
    self.frame = CGRectMake(currentFrame.origin.x, currentFrame.origin.y, emojiLabel.bounds.size.width, 15);
}

#pragma mark - setter -
- (void)setTopItem:(CTFollowList *)item
{
    if (emojiLabel != nil) {
        [emojiLabel removeFromSuperview];
        emojiLabel = nil;
        emojiLabel.delegate = nil;
    }
    
    emojiLabel = [[MLEmojiLabel alloc]init];
    emojiLabel.numberOfLines = 0;
    emojiLabel.font = [UIFont systemFontOfSize:15.0f];
    emojiLabel.emojiDelegate = self;
    emojiLabel.backgroundColor = [UIColor clearColor];
    emojiLabel.lineBreakMode = NSLineBreakByCharWrapping;
    emojiLabel.isNeedAtAndPoundSign = YES;
    [self addSubview:emojiLabel];
    
    
    @try {
        
        follwItem = item;
        NSString * describeStr = [NSString string];
        topicArray = item.topic_list;
        NSMutableAttributedString * describeAttributeStr;
        if (topicArray.count == 0) {
            
            
            if ([[CTTextManager shareInstance] isBlankString:item.text] == NO)
            {
                describeStr = [describeStr stringByAppendingString:item.text];
                describeAttributeStr = [[NSMutableAttributedString alloc] initWithString:describeStr];
                [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0] range:NSMakeRange(0,item.text.length)];
                [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(0,item.text.length)];
                [emojiLabel showAttributeLabel:describeAttributeStr];
                describeStr = nil;
            }
            else{
                self.frame = CGRectMake(currentFrame.origin.x, currentFrame.origin.y,CT_HEADER_SCREEN_WIDTH, 0);
                return;
            }
            
        }
        else if (topicArray.count == 1)
        {
            
            CTTopic * topicItem_1 = (CTTopic *)[topicArray objectAtIndex:0];
            NSString * sharp_1 = [NSString string];
            if ([[CTTextManager shareInstance] isBlankString:topicItem_1.topic] == NO)
            {
                sharp_1 = [NSString stringWithFormat:@"#%@#",topicItem_1.topic];
                describeStr = [describeStr stringByAppendingString:sharp_1];
            }
            
            if ([[CTTextManager shareInstance] isBlankString:item.text] == NO);
            {
                describeStr = [describeStr stringByAppendingString:item.text];
            }
            
            describeAttributeStr = [[NSMutableAttributedString alloc] initWithString:describeStr];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255.0 green:212/255.0 blue:103/255.0 alpha:1.0] range:NSMakeRange(0,sharp_1.length)];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0] range:NSMakeRange(sharp_1.length,item.text.length)];
            
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(0,sharp_1.length)];
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(sharp_1.length,item.text.length)];
            
            
            [emojiLabel showAttributeLabel:describeAttributeStr];
            
            describeStr = nil;
            sharp_1 = nil;
            
            
        }
        else if (topicArray.count == 2)
        {
            
            CTTopic * topicItem_1 = (CTTopic *)[topicArray objectAtIndex:0];
            NSString * sharp_1 = [NSString string];
            if ([[CTTextManager shareInstance] isBlankString:topicItem_1.topic] == NO)
            {
                sharp_1 = [NSString stringWithFormat:@"#%@#",topicItem_1.topic];
                describeStr = [describeStr stringByAppendingString:sharp_1];
            }
            
            CTTopic * topicItem_2 = (CTTopic *)[topicArray objectAtIndex:1];
            NSString * sharp_2 = [NSString string];
            if ([[CTTextManager shareInstance] isBlankString:topicItem_2.topic] == NO)
            {
                sharp_2 = [NSString stringWithFormat:@"#%@#",topicItem_2.topic];
                describeStr = [describeStr stringByAppendingString:sharp_2];
            }
            
            
            if ([[CTTextManager shareInstance] isBlankString:item.text] == NO);
            {
                describeStr = [describeStr stringByAppendingString:item.text];
            }
            
            describeAttributeStr = [[NSMutableAttributedString alloc] initWithString:describeStr];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255.0 green:212/255.0 blue:103/255.0 alpha:1.0] range:NSMakeRange(0,sharp_1.length)];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255.0 green:212/255.0 blue:103/255.0 alpha:1.0] range:NSMakeRange(sharp_1.length,sharp_2.length)];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0] range:NSMakeRange(sharp_1.length + sharp_2.length,item.text.length)];
            
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(0,sharp_1.length)];
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(sharp_1.length,sharp_2.length)];
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(sharp_1.length + sharp_2.length,item.text.length)];
            
            
            [emojiLabel showAttributeLabel:describeAttributeStr];
            
            
            describeStr = nil;
            sharp_1 = nil;
            sharp_2 = nil;
            
            
            
        }else{
           
            CTTopic * topicItem_1 = (CTTopic *)[topicArray objectAtIndex:0];
            NSString * sharp_1 = [NSString string];
            if ([[CTTextManager shareInstance] isBlankString:topicItem_1.topic] == NO)
            {
                sharp_1 = [NSString stringWithFormat:@"#%@#",topicItem_1.topic];
                describeStr = [describeStr stringByAppendingString:sharp_1];
            }
            
            CTTopic * topicItem_2 = (CTTopic *)[topicArray objectAtIndex:1];
            NSString * sharp_2 = [NSString string];
            if ([[CTTextManager shareInstance] isBlankString:topicItem_2.topic] == NO)
            {
                sharp_2 = [NSString stringWithFormat:@"#%@#",topicItem_2.topic];
                describeStr = [describeStr stringByAppendingString:sharp_2];
            }
            
            CTTopic * topicItem_3 = (CTTopic *)[topicArray objectAtIndex:2];
            NSString * sharp_3 = [NSString string];
            if ([[CTTextManager shareInstance] isBlankString:topicItem_3.topic] == NO)
            {
                sharp_3 = [NSString stringWithFormat:@"#%@#",topicItem_3.topic];
                describeStr = [describeStr stringByAppendingString:sharp_3];
            }
            
            
            if ([[CTTextManager shareInstance] isBlankString:item.text] == NO);
            {
                describeStr = [describeStr stringByAppendingString:item.text];
            }
            
            describeAttributeStr = [[NSMutableAttributedString alloc] initWithString:describeStr];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255.0 green:212/255.0 blue:103/255.0 alpha:1.0] range:NSMakeRange(0,sharp_1.length)];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255.0 green:212/255.0 blue:103/255.0 alpha:1.0] range:NSMakeRange(sharp_1.length,sharp_2.length)];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255.0 green:212/255.0 blue:103/255.0 alpha:1.0] range:NSMakeRange(sharp_1.length + sharp_2.length,sharp_3.length)];
            [describeAttributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0] range:NSMakeRange(sharp_1.length + sharp_2.length + sharp_3.length,item.text.length)];
            
            
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(0,sharp_1.length)];
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(sharp_1.length,sharp_2.length)];
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(sharp_1.length + sharp_2.length,sharp_3.length)];
            
            [describeAttributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(sharp_1.length + sharp_2.length + sharp_3.length,item.text.length)];
            
            
            [emojiLabel showAttributeLabel:describeAttributeStr];
            
            describeStr = nil;
            sharp_1 = nil;
            sharp_2 = nil;
            sharp_3 = nil;
            
        }
    
    }
    @catch (NSException *exception) {
         [CTDebugManager showErrorLogInfo:@"话题拼接出现崩溃错误！"];
    }
    
    
    emojiLabel.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH-20, 15);
    [emojiLabel sizeToFit];
 
    self.frame = CGRectMake(currentFrame.origin.x, currentFrame.origin.y, emojiLabel.bounds.size.width, emojiLabel.bounds.size.height);
    
}



#pragma mark - emoji label delegate -

- (void)mlEmojiLabel:(MLEmojiLabel*)emojiLabel didSelectLink:(NSString*)link withType:(MLEmojiLabelLinkType)type
{
   
    
    @try {
        if (type == MLEmojiLabelLinkTypePoundSign) {
            
            for (CTTopic * topicItem in topicArray) {
                NSString * linkStr = (NSString *)[link stringByReplacingOccurrencesOfString:@"#" withString:@""];
                if ([topicItem.topic isEqualToString:linkStr]) {
                    if ([self.delegate respondsToSelector:@selector(selectTopicAction:withItem:)]) {
                        [self.delegate selectTopicAction:self withItem:topicItem];
                    }
                    break;
                }
            }
        }
        
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"点击话题出现崩溃错误！"];
    }
}


@end
